package com.tmt.im.manager.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.tmt.springboot.common.restful.ResultWrapper;
import com.tmt.im.common.yunxin.YunXinHelper;
import com.tmt.im.manager.config.beans.YunXinProperties;
import com.tmt.im.manager.service.YunXinService;
import com.tmt.springboot.common.restful.ExecuteResult;
import com.tmt.springboot.common.restful.IExecuteResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * 云信平台回调接口
 *
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/4 11:18
 * @Version 1.0
 **/
@Async("asyncTaskExecutor")
@Tag(name = "云信平台回调接口", description = "云信平台回调接口")
@RestController
@RequestMapping("${api-url-prefix}/yunxin")
public class YunXinController {

    private final YunXinService yunXinService;

    private final YunXinProperties yunXinProperties;

    public YunXinController(YunXinProperties yunXinProperties,
                            YunXinService yunXinService) {
        this.yunXinProperties = yunXinProperties;
        this.yunXinService = yunXinService;
    }

    @Operation(summary = "云信平台事件通知回调接口", description = "云信平台事件通知回调接口")
    @PostMapping(value = "/eventNotify")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> processYunXinEventNotify(HttpServletRequest request,
                                                                                              @RequestBody JSONObject event) {
//        IExecuteResult<Boolean> checkResult = YunXinHelper.checkRequest(yunXinProperties.getAppKey(), yunXinProperties.getAppSecret(), request);
//        if (checkResult.getCode() != ExecuteResult.SUCCESS_CODE) {
//            return CompletableFuture.completedFuture(checkResult);
//        }
        yunXinService.processYunXinEventNotify(event);
        return CompletableFuture.completedFuture(ResultWrapper.from(ExecuteResult.success(true)));
    }

}
